
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.app.order.ISubsidyOrderAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.vo.openapi.order.resp.AcctRefundOrderApiResp;
import com.hlkj.pay.vo.openapi.order.resp.SubsidyOrderApiResp;
import com.hlkj.pay.vo.openapi.order.resp.SubsidyRefundOrderApiResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.*;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "营销补贴-订单")
@RestController
@RequestMapping("/openapi/subsidy")
@RequiredArgsConstructor
@Slf4j
public class OpenApiOrderSubsidyController {

    private final IOpenApiAppService openApiAppService;

    private final ISubsidyOrderAppService subsidyOrderAppService;

    @Operation(summary = "申请营销补贴")
    @PostMapping("/order/apply")
    OpenApiResult<SubsidyOrderApiResp> apply(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            SubsidyOrderParamReq paramReqParams = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), SubsidyOrderParamReq.class);
            CommonResult<SubsidyOrderApiResp> payOrderRespCommonResult = subsidyOrderAppService.submitSubsidyOrder(paramReqParams);
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "营销补贴查询")
    @PostMapping("/order/query")
    OpenApiResult<SubsidyOrderApiResp> queryApply(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            SubsidyOrderQueryParamReq paramReqParams = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), SubsidyOrderQueryParamReq.class);
            CommonResult<SubsidyOrderApiResp> commonResult = subsidyOrderAppService.querySubsidyOrder(paramReqParams);
            CommonResult<SubsidyOrderApiResp> payOrderRespCommonResult=new CommonResult<>();
            if(commonResult.isSuccess()){
                SubsidyOrderApiResp resultData = commonResult.getData();
                SubsidyOrderApiResp subsidyOrderApiResp = new SubsidyOrderApiResp();
                subsidyOrderApiResp.setPayOrderNo(resultData.getPayOrderNo());
                subsidyOrderApiResp.setMchOrderNo(resultData.getMchOrderNo());
                subsidyOrderApiResp.setBatchNo(resultData.getBatchNo());
                subsidyOrderApiResp.setDetailResp(resultData.getDetailResp());
                payOrderRespCommonResult.setData(subsidyOrderApiResp);
            }else {
                payOrderRespCommonResult = CommonResult.error(commonResult.getCode(),commonResult.getMsg());
            }
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "营销补贴退款")
    @PostMapping("/order/refund")
    OpenApiResult<SubsidyOrderApiResp> refund(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            SubsidyRefundOrderParamReq paramReqParams = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), SubsidyRefundOrderParamReq.class);
            CommonResult<SubsidyRefundOrderApiResp> payOrderRespCommonResult = subsidyOrderAppService.refundSubsidyOrder(paramReqParams);
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "营销补贴退款查询")
    @PostMapping("/order/refund/query")
    OpenApiResult<AcctRefundOrderApiResp> queryRefund(@Validated @RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            SubsidyRefundOrderQueryParamReq paramReqParams = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), SubsidyRefundOrderQueryParamReq.class);
            CommonResult<SubsidyRefundOrderApiResp> payOrderRespCommonResult = subsidyOrderAppService.queryRefundSubsidyOrder(paramReqParams);
            return openApiAppService.sign(payOrderRespCommonResult);
        }
        catch (Exception e) {
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

}
